Page({
  data: {
    components:[
      {
        device:'设备1',
        count:3,
        hour:"12",
        minute:"21",
        time: "12:21",
        days: "周一  周二  周三  周四  周五  周六",
        todayfood:3,
        feedCount: "喂食份数：3",
        status:[true,true,true,true,true,true,false],
      },
      {
        device:'设备2',
        count:3,
        hour:"17",
        minute:"30",
        time: "17:30",
        days: "每天",
        todayfood:3,
        feedCount: "喂食份数：3",
        status:[true,true,true,true,true,true,true],
      },
    ]
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp(); 
    const sharedData = app.globalData.sharedData;
    console.log(sharedData);
    const flag = app.globalData.shareFlag;
    const index1 = app.globalData.editingComponentIndex;
    if (sharedData) {
      const daysOfWeek = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
      let selectedDays = '';
      let status1 = [false,false,false,false,false,false,false];
      if(!sharedData.status){
        return;
      }
      sharedData.status.forEach((isDaySelected, index) => {
        if (isDaySelected) {
          selectedDays += daysOfWeek[index] + ' ';
          status1[index] = true;
        }
      });
      if (sharedData.status.every(isDaySelected => isDaySelected === true)) {
        selectedDays = '每天';
      }
      const hour1 = sharedData.value[1] !== null ? sharedData.value[1] : '0';
      const minute1 = sharedData.value[2] !== null ? sharedData.value[2] : '0';
      if (flag == 1) {
        const components = this.data.components;
        if (sharedData.device) { 
          components[index1] = {
            device: sharedData.device,
            hour: hour1,
            minute: minute1,
            time: sharedData.value[1] + ':' + sharedData.value[2],
            days: selectedDays.trim(),
            todayfood: sharedData.todayfood,
            status: status1,
          };
        }else{
          components[index1] = {
            device:components[index1].device,
            hour: hour1,
            minute: minute1,
            time: sharedData.value[1] + ':' + sharedData.value[2],
            days: selectedDays.trim(),
            todayfood:sharedData.todayfood,
            status: status1,
          };
        }
        console.log(components[index1]);
        this.setData({
          components: components
        });
      } else if (flag === 0) {
        const newComponent = {
          device:sharedData.device,
          hour: sharedData.value[1],
          minute: sharedData.value[2],
          time: sharedData.value[1] + ':' + sharedData.value[2],
          days: selectedDays.trim(),
          todayfood:sharedData.todayfood,
          status: status1,
        };
        const components = this.data.components;
        components.push(newComponent);
        this.setData({
          components: components
        });
      }
      delete app.globalData.sharedData;
      delete app.globalData.editingComponentIndex;
      delete app.globalData.shareFlag;
      delete app.globalData.componentToEdit;
    } 
  },  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  changeinfo:function(event){
    const index = event.currentTarget.dataset.index;
    const componentToEdit = this.data.components[index];
    getApp().globalData.componentToEdit = componentToEdit;
    getApp().globalData.editingComponentIndex = index;
    wx.navigateTo({
      url: '/pages/addfood/addfood?index=' + index,
    });
  },
  switchChange:function(event){
    var isChecked = event.detail.value;
    console.log(isChecked);
    if(isChecked){
      const index = event.currentTarget.dataset.index; 
      console.log(this.data.components[index]);
      wx.request({
        url: 'timeset',
        data:{"id":this.data.components[index].device,
              "food":this.data.components[index].count,
              "hour":this.data.components[index].hour,
              "minute":this.data.components[index].minute,
              "day":this.data.components[index].status},
        method:'POST'
      })
    }
  }
})
